package sale.wxb.loveshopping.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sale.wxb.loveshopping.entity.common.Result;
import sale.wxb.loveshopping.service.IVerifyCodeService;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping("verifyCode")
@Api(tags = { "验证码" })
@Validated
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("sendEmailCode")
    @ApiOperation("发送邮箱验证码")
    public Result<Boolean> sendEmailCode(@RequestParam @NotBlank @Email String email, @RequestParam(defaultValue = "false") Boolean registered) {
        return Result.success(verifyCodeService.sendVerifyCodeByEMail(email, registered));
    }

    @GetMapping("sendPhoneSms")
    @ApiOperation("发送手机短信验证码")
    public void sendPhoneSms() {

    }

    @GetMapping(value = "getImageCode", produces = MediaType.IMAGE_PNG_VALUE)
    @ApiOperation(value = "获取图形验证码", notes = "该接口会直接返回图片二进制流\r\n基于session验证 前端请求如跨域时注意允许携带cookie")
    public byte[] getImageCode() {
        return verifyCodeService.getImageVerifyCode();
    }

    @GetMapping("getJigsawPuzzle")
    @ApiOperation("获取拼图验证码")
    public void getJigsawPuzzle() {

    }

    @GetMapping("verifyEmailCode")
    @ApiOperation("验证邮箱验证码")
    public Result<Boolean> verifyEmailCode(@RequestParam @NotBlank @Email String email, @RequestParam @NotBlank String code) {
        return Result.success(verifyCodeService.verifyEmailCode(email, code));
    }

    @GetMapping("verifyPhoneSms")
    @ApiOperation("验证手机短信验证码")
    public void verifyPhoneSms() {

    }

    @GetMapping("verifyImageCode")
    @ApiOperation(value = "验证图形验证码", notes = "基于session验证 前端请求如跨域时注意允许携带cookie")
    public Result<Boolean> verifyImageCode(@RequestParam @NotBlank String code) {
        return Result.success(verifyCodeService.verifyImageCode(code));
    }
}
